import { Component, OnInit, ViewChild, Input } from '@angular/core';
import { STComponent, STColumn } from '@delon/abc';
import { MajorDetailComponent } from '../major-detail/major-detail.component';
import { ObjectUtil } from 'src/app/benshuo/util/data/object-util';
import { TABLE_PARAMETER } from 'src/app/benshuo/util/data/table-parameter';
import { QueryOptions } from 'src/app/benshuo/util/model/query-options';
import { MajorDTO } from 'src/app/benshuo/model/major-dto';
import { MajorService } from 'src/app/benshuo/services/major.service';
import { NzMessageService } from 'ng-zorro-antd';
import { OperationNotification } from 'src/app/benshuo/util/data/operation-notification';

@Component({
  selector: 'app-major-list',
  templateUrl: './major-list.component.html',
  styleUrls: ['./major-list.component.less'],
})
export class MajorListComponent implements OnInit {
  /**
   * 表格组件
   */
  @ViewChild('st', null)
  st: STComponent;

  /**
   * 详情组件
   */
  @ViewChild('majorDetailComponent', null)
  majorDetailComponent: MajorDetailComponent;

  /**
   * 调用api
   */
  loading = false;

  /**
   * 列表参数
   */
  TABLE_PARAMETER = ObjectUtil.deepClone(TABLE_PARAMETER);

  /**
   * 分页，排序参数
   */
  @Input()
  queryOptions: QueryOptions = {
    page: 0,
    size: 20,
    sort: 'id,asc',
  };

  /**
   * 表格数据数组
   */
  tableData: Array<MajorDTO>;

  /**
   * 表格的列定义
   */
  @Input()
  stColumns: STColumn[] = [
    {
      title: '序号',
      render: 'number',
      width: '50px',
      className: 'text-center',
    },
    {
      title: '专业名称',
      index: 'name',
      width: '15%',
      className: 'text-left',
    },
    {
      title: '科类',
      index: 'families',
      width: '15%',
      className: 'text-left',
    },
    {
      title: '操作',
      render: 'operations',
      width: '120px',
      className: 'text-center',
    },
  ];
  constructor(private service: MajorService, private msg: NzMessageService) {}

  ngOnInit() {
    this.load();
  }

  /**
   * 表格变化事件，用于双击、排序，翻页等操作
   */
  change(e: any): void {
    // 双击事件
    if (e.type === 'dblClick') {
      this.update(e.dblClick.item);
    }

    // 排序事件
    if (e.type === 'sort') {
      if (e.sort.map) {
        const temp = e.sort.map.sort;
        if (temp) {
          this.queryOptions.sort = temp.replace(/[\.-]/g, ',');
        }
      }
      this.load();
    }

    // 翻页设置
    if (e.type === 'ps' || e.type === 'pi') {
      this.queryOptions.page = e.pi - 1;
      this.queryOptions.size = e.ps;
      this.load();
    }
  }

  load(): void {
    this.loading = true;
    // tslint:disable-next-line: deprecation
    this.service.getOnePage(this.queryOptions).subscribe(
      data => {
        if (data) {
          this.tableData = data.data;
          this.TABLE_PARAMETER.page.total = data.totalRecords;
        }
      },
      null,
      () => {
        this.loading = false;
      },
    );
  }

  /***
   * 编辑
   */
  update(item: MajorDTO): void {
    this.loading = true;
    this.service.findById(item.id).subscribe(
      data => {
        if (data) {
          this.loading = false;
          this.majorDetailComponent.edit(data);
        }
      },
      () => {
        this.loading = false;
      },
      () => {
        this.loading = false;
      },
    );
  }

  /**
   * 创建数据
   */
  create(): void {
    this.majorDetailComponent.edit(null);
  }

  /**
   * 删除
   */
  delete(item: MajorDTO): void {
    this.loading = true;
    // tslint:disable-next-line: deprecation
    this.service.deleteById(item.id).subscribe(
      data => {
        this.st.removeRow(item);
        this.st.total -= 1;
        this.msg.success('删除数据成功');
      },
      null,
      () => {
        this.loading = false;
      },
    );
  }

  /**
   * 增删改 操作通知事件
   *
   * @param $event 操作通知事件
   */
  notify($event: OperationNotification<MajorDTO>): void {
    this.load();
  }
}
